package Files;

import Utils.MSJ_JOptionPane;
import Security.MSJ_Encryption;
import Security.MSJ_EncryptionType;
import java.io.*;

/**
 * <b>Use this class to Read and Write with files with Encryption and
 * without.</b>
 *
 * @author Mohamed Tarek Salama
 * @version 1.1 BETA
 */
public class MSJ_Files {

    /**
     * Read text from file stored in storage device<br> Make sure that the path
     * is correct
     *
     * @param path : the path of the file that you want to read from.
     * @return : String
     */
    public String readfile(String path) {
        String st, output = "";
        try {
            BufferedReader br = new BufferedReader(new FileReader(path));
            while ((st = br.readLine()) != null) {
                output += st;
            }
        } catch (IOException ex) {
            MSJ_JOptionPane.show_Error_Message("Error read from file Or file not found\n" + ex.toString());
        } finally {
            return output;
        }
    }

    /**
     * Write text to file stored in storage device<br> Make sure that the path
     * is correct.
     *
     * @param path : path of the file that will create in it.
     * @param txt : the text that will be written in the file.
     */
    public void writefile(String path, String txt) {
        BufferedWriter bw;
        try {
            bw = new BufferedWriter(new FileWriter(path));
            bw.write(txt);
            bw.close();
        } catch (IOException ex) {
            MSJ_JOptionPane.show_Error_Message("Error write in the file Or file not found\n" + ex.toString());
        }
    }

    /**
     * Read Encrypted text from file <b>that encrypted by "write_Enc" method
     * only</b> and stored in storage device<br> Make sure that the path is
     * correct.
     *
     * @param path : the path of the file that you want to read from.
     * @return String
     */
    public String read_Enc(String path, MSJ_EncryptionType enc_type) {
        String st, output = "";
        try {
            BufferedReader br = new BufferedReader(new FileReader(path));
            while ((st = br.readLine()) != null) {
                if (enc_type.toString().equals("mono")) {
                    output += new MSJ_Encryption().decrypt_mono(st);
                } else if (enc_type.toString().equals("DES")) {
                    output += new MSJ_Encryption().decrypt_DES(st);
                }
            }
        } catch (IOException ex) {
            MSJ_JOptionPane.show_Error_Message("Error read from file\n" + ex.toString());
        } finally {
            return output;
        }
    }

    /**
     * Write Encrypted text to file and <b> must read by "read_Enc" method
     * only</b> and stored in storage device<br> Make sure that the path is
     * correct.
     *
     * @param path : path of the file that will create in it.
     * @param txt : the text that will be written in the file.
     */
    public void write_Enc(String path, String txt, MSJ_EncryptionType enc_type) {
        BufferedWriter bw;
        try {
            bw = new BufferedWriter(new FileWriter(path));
            if (enc_type.toString().equals("mono")) {
                bw.write(new MSJ_Encryption().encrypt_mono(txt));
            } else if (enc_type.toString().equals("DES")) {
                bw.write(new MSJ_Encryption().encrypt_DES(txt));
            }
            bw.close();
        } catch (IOException ex) {
            MSJ_JOptionPane.show_Error_Message("Error write in the file\n" + ex.toString());
        }
    }
}